package com.overoid.diary.ui.picture;

import java.util.ArrayList;


import com.overoid.diary.R;
import com.overoid.diary.ui.DayActivity;
import com.overoid.diary.ui.common.ActivityImpl;
import com.overoid.diary.ui.common.DBHelper;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


public class PictureActivity extends ActivityImpl { 
    
	
	private int totalImgCnt = 0;
	private int imgPosition = 0;
	private ArrayList<Bitmap> imgs = new ArrayList<Bitmap>();
	private ImageView imgView =null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
			
		super.onCreate(savedInstanceState);			
		
		setContentView(R.layout.day_picture);
		
		Intent in = getIntent();
		
		//Log.v("=============================",);
		// data 조회
		DBHelper db = new DBHelper(getApplicationContext());
		Cursor c = db.getPictureMediaList(in.getIntExtra("memo_id",0));
		
		c.moveToFirst();
		for(int i=0; i<c.getCount();i++){
			
			BitmapFactory.Options bitmpaOption = new BitmapFactory.Options();
			Bitmap bitmap = BitmapFactory.decodeFile(c.getString(1), bitmpaOption);
			
			imgs.add(bitmap);
			c.moveToNext();
			
		}
		totalImgCnt = c.getCount();		
		if (c != null && !c.isClosed()) {
			c.close();
		}
		
		
		
		final Button dayPictureBtnNext = (Button)findViewById(R.id.dayPictureBtnNext);
		dayPictureBtnNext.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setImageView("NEXT",(Button)v);
			}
		});
		
		final Button dayPictureBtnPre = (Button)findViewById(R.id.dayPictureBtnPre);
		dayPictureBtnPre.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setImageView("PRE",(Button)v);
			}
		});
		
		final ImageButton btnDone = (ImageButton)findViewById(R.id.dayPicImgBtnDone);
		btnDone.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		// 초기값 setting
		if(totalImgCnt !=0){
			imgView=(ImageView)findViewById(R.id.dayPictureImgView);
			imgView.setImageBitmap(imgs.get(0));
			imgPosition=1;
			if(totalImgCnt > 1){
				dayPictureBtnNext.setVisibility(View.VISIBLE);
			}
			
			((TextView)findViewById(R.id.dayPicTxtTitle)).setText(imgPosition+" / "+totalImgCnt);
			
		}
	} 
	
	private void setImageView(String type,Button btn){
		if(type.equals("NEXT")){
			imgPosition++;
			((Button)findViewById(R.id.dayPictureBtnPre)).setVisibility(View.VISIBLE);
			
			if(totalImgCnt == imgPosition){
				btn.setVisibility(View.INVISIBLE);
				
			}
			
		}else if(type.equals("PRE")){
			imgPosition--;
			((Button)findViewById(R.id.dayPictureBtnNext)).setVisibility(View.VISIBLE);
			if(1 == imgPosition){
				btn.setVisibility(View.INVISIBLE);
			}
		}
		
		((TextView)findViewById(R.id.dayPicTxtTitle)).setText(imgPosition+" / "+totalImgCnt);
		imgView.setImageBitmap(imgs.get(imgPosition-1));
	}
	
	
	
 
 
}
